package com.joysuch.wwyt.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joysuch.wwyt.edu.mapper.EduVideoBreakpointResumeMapper;
import com.joysuch.wwyt.edu.bean.EduVideoBreakpointResumeParam;
import com.joysuch.wwyt.edu.entity.EduVideoBreakpointResume;
import com.joysuch.wwyt.edu.service.EduVideoBreakpointResumeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
@Slf4j
@Transactional
public class EduVideoBreakpointResumeServiceImpl implements EduVideoBreakpointResumeService {

    @Autowired
    private EduVideoBreakpointResumeMapper mapper;


    @Override
    public void add(EduVideoBreakpointResume eduVideoBreakpointResume) {
        EduVideoBreakpointResume eduVideoBreakpointResume2 = mapper.selectOne(new QueryWrapper<EduVideoBreakpointResume>().eq("user_id", eduVideoBreakpointResume.getUserId()).eq("video_id", eduVideoBreakpointResume.getVideoId()));
        if (eduVideoBreakpointResume2 != null && eduVideoBreakpointResume.getWatchTime()!= null){
            eduVideoBreakpointResume2.setWatchTime(eduVideoBreakpointResume.getWatchTime());
            mapper.updateById(eduVideoBreakpointResume2);
        } else if (eduVideoBreakpointResume2 == null && eduVideoBreakpointResume.getWatchTime() != null) {
            mapper.insert(eduVideoBreakpointResume);
        }
    }

    @Override
    public String getEduVideoBreakpointResume(EduVideoBreakpointResumeParam param) {
        EduVideoBreakpointResume eduVideoBreakpointResume = mapper.selectOne(new QueryWrapper<EduVideoBreakpointResume>().eq("user_id", param.getUserId()).eq("video_id", param.getVideoId()));
        String watchTime = "0";
        if (eduVideoBreakpointResume!=null && eduVideoBreakpointResume.getWatchTime()!=null){
            watchTime = eduVideoBreakpointResume.getWatchTime();
        }
        return watchTime;
    }
}
